package cn.itsource.basic.exception;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.ErrorCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@ControllerAdvice
@RestControllerAdvice //作用到所以加了@RequestMapping注解的类=controller
public class GlobalExceptionHandler {
    //捕捉BusinessException异常，只要conroller的方法中抛出BusinessException，就会执行当前方法
    @ExceptionHandler(BusinessException.class)
    public AjaxResult businessException(BusinessException e){
        //打印错误信息
        //e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
    }
    //1.作用到controller - @RestControllerAdvice
    //2.捕捉指定的异常 - @ExceptionHandler(BusinessException.class)
    //3.响应处理结果

    //捕捉其他异常，只要conroller的方法中抛出非BusinessException，就会执行当前方法
    @ExceptionHandler(Exception.class)
    public AjaxResult exception(Exception e){
        //打印错误信息
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage(ErrorCode.SYSTEM_ERROR.getMessage());
    }
    //匹配规则：不是先后顺序，是精准度匹配
}
